package com.wsc.service.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.wsc.entity.Teacher;
import com.wsc.repository.TeacherRepository;
import com.wsc.service.TeacherService;

@Service
public class TeacherServiceimpl implements TeacherService {
	@Autowired
	private TeacherRepository tr;

	@Override
	public Integer add(Teacher t) {
		try {
			//禁止重复卡号添加数据
			List<Teacher> list = tr.findAll();
			if (list.size() > 0) {
				for (Integer i = 0; i < list.size(); i++) {
					if (t.getCardno().equals(list.get(i).getCardno())) {
						return 0;
					}
				}
			}
			tr.save(t);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Integer deleteById(Integer id) {
		try {
			// 使用jpaRepository的deleteById的方法会把关联的另一张表也删除一条数据
			Teacher t = new Teacher();
			t.setId(id);
			tr.delete(t);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Integer update(Teacher t) {
		try {
			//禁止修改卡号为已存在的
			List<Teacher> list = tr.findAll();
			if (list.size() > 0) {
				for (Integer i = 0; i < list.size(); i++) {
					if (t.getCardno().equals(list.get(i).getCardno())) {
						// id不同，则为两条条数据，cardid唯一的,不允许重复
						if (!t.getId().equals(list.get(i).getId())) {
							return 0;
						}
						break;
					}
				}
			}
			tr.save(t);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Map<String, Object> getAll(Map<String, Object> params) {
		Teacher teacher = (Teacher) params.get("teacher");
		Specification<Teacher> spec = getSpec(teacher);
		Integer page = (Integer) params.get("page");
		Integer limit = (Integer) params.get("limit");
		Pageable pa = PageRequest.of(page, limit);
		// 返回实体页
		Page<Teacher> res = tr.findAll(spec, pa);
		// 获取list集合数据
		List<Teacher> data = res.getContent();
		// count 数据总条数
		long count = res.getTotalElements();
		if (data.size() > 0) {
			params.put("code", 0);
			params.put("data", data);
			params.put("count", count);
		} else {
			params.put("code", 1);
			params.put("msg", "数据异常");
		}
		return params;
	}

	private Specification<Teacher> getSpec(Teacher teacher) {
		return new Specification<Teacher>() {
			@Override
			public Predicate toPredicate(Root<Teacher> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate pre = cb.conjunction();
				List<Expression<Boolean>> sqllist = pre.getExpressions();
				if (!StringUtils.isEmpty(teacher.getCardno())) {
					sqllist.add(cb.like(root.get("cardno"), "%" + teacher.getCardno() + "%"));
				}
				if (!StringUtils.isEmpty(teacher.getName())) {
					sqllist.add(cb.like(root.get("name"), "%" + teacher.getName() + "%"));
				}
				if (!StringUtils.isEmpty(teacher.getSectionid())) {
					sqllist.add(cb.equal(root.get("sectionid"), teacher.getSectionid()));
				}
				if (!StringUtils.isEmpty(teacher.getStatus())) {
					sqllist.add(cb.equal(root.get("status"), teacher.getStatus()));
				}
				return pre;
			}
		};
	}

	@Override
	public Teacher findByCardno(String cardno) {
		return tr.getTeacherByCardno(cardno);
	}

	@Override
	public Teacher getOne(Integer id) {
		return tr.getOne(id);
	}

	@Override
	public void updateAllStatusToZero() {
		tr.updateAllStatusToZero();
	}

}
